import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
// 引入保存信息插件
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

const store = new Vuex.Store({
    state: {
        menuList: [],
        userInfo: {}

    },
    mutations: {
        // 保存菜单数据
        saveMenuList(state, menuList) {
            state.menuList = menuList
        },
        // 保存登录数据
        saveUserInfo(state, userInfo) {
            state.userInfo = userInfo
        },
        userlogout(state){
            state.userInfo = {}
        }
    },
    actions: {
        // 调用接口，获得菜单数据
        getMenuList({ commit }, params = {}) {
            return new Promise((resolve, reject) => {
                axios.get("/api/menulist", { params: params })
                    .then((result) => {
                        commit('saveMenuList', result.data.list)
                        resolve(result)
                    })
                    .catch((err) => {
                        console.log(err);
                        reject(err)
                    });
            })
        },

        // 用户登录
        userLogin({ commit }, params = {}) {
            return new Promise((resolve, reject) => {
                axios.post("/api/userlogin", params)
                    .then((result) => {
                        commit('saveUserInfo', result.data.list)
                        console.log(result);
                        resolve(result)
                    })
                    .catch((err) => {
                        console.log(err);
                        reject(err)
                    });
            })
        }
    },
    // 插件 createPersistState可以把store里的数据保存在localStorage
    plugins: [createPersistedState()]
})

export default store